#ifndef UI_ANIM_PLAYER_
#define UI_ANIM_PLAYER_

#include <bitset>

namespace UI_ANIMATION
{
	enum ANIM_ENUM{
		AnimEnter = 0,
		AnimExit,
		AnimNormal,
		AnimTouch,
		AnimEnterTouch,
		AnimExitTouch,
		AnimHide,
		AnimShow,
		AnimDisable,
		AnimEnable,
		AnimEnterChange,
		AnimExitChange,
		AnimChange,
		AnimCount,
	};

	enum CHANNEL_TYPE{
		ChannelTypeImage,
		ChannelTypeText,
		ChannelTypeSelfDraw,
	};

	enum TIMELINE_STYLE{
		TimelinePositionX,
		TimelinePositionY,
		TimelineRotationZ,
		TimelineScale,
		TimelineAlpha,
		TimelineCount,
	};

	enum InterpolationStyle
	{
		InterpolationLinear,
	};
}

typedef struct tgUIAnimChannelData
{
	float m_values[UI_ANIMATION::TimelineCount];
}UIAnimChannelData;

typedef struct tgUIAnimKey
{
	tgUIAnimKey(float time, float value)
	{
		m_keyTime = time;
		m_value = value;
	}
	float m_keyTime;
	float m_value;
}UIAnimKey;

typedef struct tgUITimeline
{
	tgUITimeline()
	{
		m_keys.clear();
	}
	tgUITimeline(UI_ANIMATION::TIMELINE_STYLE style)
	{
		m_keys.clear();
		m_style = style;
	}
	void Reset()
	{
	}
	~tgUITimeline()
	{
		m_keys.clear();
	}
	UI_ANIMATION::TIMELINE_STYLE m_style;
	irr::core::array<UIAnimKey> m_keys;
}UITimeline;

typedef struct tgUIChannel
{
	tgUIChannel()
	{
		for(int i=0;i<UI_ANIMATION::TimelineCount;++i)
		{
			m_timelines[i] = NULL;
		}
	}
	tgUIChannel(UI_ANIMATION::CHANNEL_TYPE type, int targetIndex)
	{
		m_type = type;
		m_targetIndex = targetIndex;
		for(int i=0;i<UI_ANIMATION::TimelineCount;++i)
		{
			m_timelines[i] = NULL;
		}
	}
	void Init( UI_ANIMATION::CHANNEL_TYPE type, int targetIndex)
	{
		m_type = type;
		m_targetIndex = targetIndex;
	}
	void Reset()
	{
	}
	~tgUIChannel()
	{
	}
	UI_ANIMATION::CHANNEL_TYPE m_type;
	int m_targetIndex;
	UITimeline *m_timelines[UI_ANIMATION::TimelineCount];
}UIChannel;

typedef struct tgUIAnim
{
	tgUIAnim()
	{
		m_channels.clear();
	}
	tgUIAnim(bool loop, float time)
	{
		m_isLoop = loop;
		m_timeRange = time;
		m_channels.clear();
	}
	void Init(bool loop, float time)
	{
		m_isLoop = loop;
		m_timeRange = time;
	}
	void Reset()
	{
	}
	~tgUIAnim()
	{
		m_channels.clear();
	}
	bool m_isLoop;
	float m_timeRange;
	irr::core::array<UIChannel *>m_channels;
}UIAnim;

class UIAnimPlayer
{
public:
	UIAnimPlayer(bool DataByFrame = true);
	~UIAnimPlayer();

	bool IsAnimEnd(UI_ANIMATION::ANIM_ENUM animFlag);
	void PlayAnim(UI_ANIMATION::ANIM_ENUM animFlag);

	void AddAnim( UI_ANIMATION::ANIM_ENUM animIndex, UIAnim*p);

	void Update(float df);

	bool GetChannelData( UI_ANIMATION::CHANNEL_TYPE type, int targetIndex , UIAnimChannelData &result);

	bool HasAnim( UI_ANIMATION::ANIM_ENUM animFlag);

	void Reset();

	float GetCurrentTime();
private:

	UIAnim* m_anims[UI_ANIMATION::AnimCount];
	std::bitset<UI_ANIMATION::AnimCount> m_finish;

	float m_time;
	UI_ANIMATION::ANIM_ENUM m_playAnim;

	bool m_isDataByFrame;

private:
	float GetDefaultValue(int style);
	float ComputeInterpolation(float time1, float value1, float time2, float value2, float curtime, UI_ANIMATION::InterpolationStyle style = UI_ANIMATION::InterpolationLinear);
	float ComputeLinearResult(float time1, float value1, float time2, float value2, float curtime);
};

#endif//UI_ANIM_PLAYER_